<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;
class TopicController extends CommonController {
    public function config(){
        $this->assign('mark','content');
        $this->assign('umark','bbs_config');
        $this->display('config');
    }
    //列表
    public function index(){
      $topic_object = D('bbs_topic');
      $map['status'] = array('egt', '0'); // 禁用和正常状态
      $Page = new Page($topic_object->where($map)->count(),C('ADMIN_CONFIG.page_size'));
      $Page->setConfig('prev','«');
      $Page->setConfig('next','»');
      $data_list     = $topic_object
          ->where($map)
          ->order('id asc')
          ->limit($Page->firstRow.','.$Page->listRows)
          ->select();
      $this->assign('mark','content');
      $this->assign('umark','bbs_list');
      A('list')->setMetaTitle('主题列表')
          ->setBreadCrumb(array('内容','论坛管理','主题列表'))
          ->addTopButton('delete')
          ->addTableColumn('id','ID','number',50)
          ->addTableColumn('section_id','所属版块','getsectionup',80,'false')
          ->addTableColumn('user_id','发布人','getuser',80,'false')
          ->addTableColumn('title','标题','text','auto','false')
          ->addTableColumn('look_sum','浏览数','number',70)
          ->addTableColumn('reply_sum','回复数','number',70)
          ->addTableColumn('is_top','置顶','status',60)
          ->addTableColumn('is_hot','精华','status',60)
          ->addTableColumn('is_lock','锁定','status',60)
          ->addTableColumn('status','状态','status',60)
          ->addTableColumn('ctime','创建时间','date',120)
          ->addTableColumn('','操作','rightbtn',140,'false')
          ->setTableDataList($data_list)
          ->setTableDataPage($Page->show())
          ->addRightButton('edit')
          ->addRightButton('delete')
          ->fetch();
    }
    //编辑
    public function edit(){
      $topic_object = D('bbs_topic');
      if (IS_POST) {
          $data = $topic_object->create();
          if ($data) {
              $rs = $topic_object->save($data);
              if ($rs) {
                  $ajax = array(
                    'status'=>1,
                    'text'=>'主题编辑成功',
                    'url'=>U('index')
                  );
              } else {
                  $ajax = array(
                    'status'=>0,
                    'text'=>'主题编辑失败',
                    'url'=>''
                  );
              }
          }else {
              $ajax = array(
                'status'=>0,
                'text'=>$topic_object->getError(),
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
      }else {
        $this->assign('mark','content');
        $this->assign('umark','bbs_list');
        $id = I('get.ids',0,'intval');
        $form_data = $topic_object->find($id);
        $updata = array();
        $secdata = M('bbs_section')->field('id,title')->where(array('status'=>1,'upid'=>0))->select();
        foreach ($secdata as $key => $value) {
            $up['value'] = $value['id'];
            $up['title'] = $value['title'];
            $up['disabled'] = true;
            array_push($updata,$up);
            $subdata = M('bbs_section')->field('id,title')->where(array('status'=>1,'upid'=>$value['id']))->select();
            foreach ($subdata as $k => $val) {
                $u['value'] = $val['id'];
                $u['title'] = '· '.$val['title'];
                $u['disabled'] = false;
                array_push($updata,$u);
            }
        }
        A('form')->setMetaTitle('资讯编辑')
        ->setBreadCrumb(array('内容','资讯管理','资讯列表','资讯编辑'))
        ->setPostUrl(U('edit'))
        ->addFormItem('id','hidden')
        ->addFormItem('title','text','资讯标题','',array(),array('required'=>true))
        ->addFormItem('section_id','select','所属版块','',$updata)
        ->addFormItem('content','simditor','主题正文','')
        ->addFormItem('look_sum','text','浏览量')
        ->addFormItem('is_top','switch','置顶','',array('确认','取消'))
        ->addFormItem('is_hot','switch','加精','',array('确认','取消'))
        ->addFormItem('is_lock','switch','锁定','',array('确认','取消'))
        ->addFormItem('status','switch','状态','',array('启用','禁用'))
        ->setFormData($form_data)
        ->fetch();
      }
    }
    //删除
    public function delete(){
        if (IS_GET) {
          $ids = I('get.ids');
          $topic_object = M('bbs_topic');
          $map = array(
            'id' => array('in',$ids),
          );
          $rs = $topic_object->where($map)->delete();
          if ($rs) {
              $ajax = array(
                'status'=>1,
                'text'=>'主题删除成功',
                'url'=>U('index')
              );
          } else {
              $ajax = array(
                'status'=>0,
                'text'=>'主题删除失败',
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
        }
    }
}
